<template>
  <div class="table-box">
    <ProTable ref="proTable" title="登录日志" :columns="columns" :request-api="getUserOperationLogList">
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="Delete" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button type="primary" link :icon="View" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <SysTaskLogDialog ref="sysTaskLogDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="sysUserOperaTionLog">
import { ref } from "vue";
import { System } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import SysTaskLogDialog from "@/views/system/components/SysTaskLogDialog.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { View, Delete } from "@element-plus/icons-vue";
import { getUserOperationLogList, deleteTaskLog } from "@/api/modules/system";
import { taskLogStatus } from "@/utils/serviceDict";

const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps<System.ResTaskLogList>[] = [
  { prop: "id", label: "序列", width: 80 },
  { sortable: true, prop: "name", label: "任务名称", align: "left", search: { el: "input" }, minWidth: 300 },
  {
    sortable: true,
    prop: "createTime",
    label: "完成时间",
    align: "left",
    minWidth: 164,
    search: {
      el: "date-picker",
      props: {
        type: "datetimerange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD"
      }
    }
  },
  { sortable: true, prop: "isSuccess", label: "是否完成", enum: taskLogStatus, search: { el: "select" }, minWidth: 100 },
  {
    sortable: true,
    prop: "parameters",
    label: "其他数据",
    minWidth: 280,
    align: "left",
    search: { el: "input" }
  },
  { prop: "operation", label: "操作", fixed: "right", width: 150 }
];

// 删除
const deleteAccount = async (params: System.ResTaskLogList) => {
  await useHandleData(deleteTaskLog, { id: params.id }, `删除【${params.logTitle}】定时任务`);
  proTable.value?.getTableList();
};

// 打开 dialog(新增、查看、编辑)
const sysTaskLogDialogRef = ref<InstanceType<typeof SysTaskLogDialog> | null>(null);
const openDialog = (title: string, row: Partial<System.ResTaskLogList> = {}) => {
  const params = {
    title,
    isView: true,
    row: { ...row }
  };
  sysTaskLogDialogRef.value?.acceptParams(params);
};
</script>
